package com.example.userpage.repository;

import com.example.userpage.model.ImageVote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class ImageVotesRepository {
    @Autowired
    private JdbcTemplate jdbcVotes;

    public List<ImageVote> findByCreatorOrderByVotes(String creator) {
        String sql = "SELECT id, creator, image_name, vote_count FROM images WHERE creator = ? ORDER BY vote_count DESC";
        return jdbcVotes.query(sql, new Object[]{creator}, (rs, rowNum) -> {
            ImageVote iv = new ImageVote();
            iv.setId(rs.getLong("id"));
            iv.setCreator(rs.getString("creator"));
            iv.setImage_name(rs.getString("image_name"));
            iv.setVote_count(rs.getInt("vote_count"));
            return iv;
        });
    }
}